import { router } from './router'
//引入进度条
//@ts-ignore
import Nprogress from 'nprogress'
//引入进度条的样式
import 'nprogress/nprogress.css'
// 引入仓库
import { useUserStore } from '@/stores/modules/user/user'
import { pinia } from '@/main'

let noTokenGO = [
  '/home',
  '/hospital/registered',
  '/hospital/detail',
  '/hospital/notice',
  '/hospital/inform',
  '/hospital/opera'
]

// 前置路由守卫
router.beforeEach((to, from, next) => {
  let userSore = useUserStore(pinia)
  // 加载条
  Nprogress.start()
  //动态设置网页左上角的标题
  document.title = `尚医通-${to.meta.title}`
  Nprogress.configure({
    trickleSpeed: 300,
    showSpinner: false
  })
  if (userSore.token) {
    next()
  } else {
    if (noTokenGO.includes(to.path)) {
      next()
    } else {
      // 显示登陆页面
      userSore.dialogTableVisible = true
      // 存储fullPath
      userSore.redirect = to.fullPath
      console.log(userSore.redirect)
      next({ path: '/home', query: { redirect: to.fullPath } })
    }
  }
})

// 后置路由守卫
router.afterEach((to, from, next) => {
  Nprogress.done()
})
